package com.shyroke.controller;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.shyroke.calculation.AttributeWeightCal;
import com.shyroke.calculation.ExpertWeightCal;
import com.shyroke.calculation.GroupDecisionInfoCal;
import com.shyroke.calculation.QiCal;
import com.shyroke.calculation.SiCal;
import com.shyroke.util.ConvertUtil;

import entity.ConvertExpert;
import entity.Expert;
import entity.GroupDesicionDto;

/**
 * 专家权重和属性权重相关action
 * @author shyroke
 *
 */
@Controller
@RequestMapping(value="weightAction")
public class WeightAction {
	
	/**
	 * 获取专家权重和属性权重
	 * @param model
	 * @return
	 * @throws InvocationTargetException 
	 * @throws IllegalArgumentException 
	 * @throws IllegalAccessException 
	 * @throws SecurityException 
	 * @throws NoSuchMethodException 
	 * @throws IOException 
	 */
	@RequestMapping(value="getWeight")
	public ModelAndView getWeight(ModelAndView model) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException {
		Map<Integer,Expert> expertMap = ExpertAction.expertMap;
		if(expertMap == null || expertMap.size() == 0) {
			throw new RuntimeException("专家的中智集不能为空");
		}
		List<ConvertExpert> convertExpertList = ConvertUtil.covert(expertMap);
		
		//  获取4个专家的重要性
		Map<String,Double> expertImportance = ExpertWeightCal.getExpertWeight();
		
		// 获取属性权重
		Map<String,Double> attributeWeight = AttributeWeightCal.calAttributeWeight(convertExpertList,expertImportance);
	
		//群体决策信息,List.size代表决策方案数量
		List<GroupDesicionDto> desicionDtos = GroupDecisionInfoCal.getGroupDesion(expertMap,expertImportance);
		
		//计算群体效用 值Si <x1,0.402>表示决策方案x1的效用值为Si
		Map<String,Double> siMap = new SiCal().getSi(attributeWeight, desicionDtos);
		
		//计算个体遗憾 值Ri <x1,0.402>表示决策方案x1的个体遗憾值为Ri
		Map<String,Double> RiMap = new SiCal().getRi(attributeWeight, desicionDtos);
		
		//计算折中评价值Qi<x1,0.77> 表示决策方案x1的评价值为0.77
		Map<String,Double> qiMap =	new QiCal(siMap, RiMap).getQi();
		Map<String,Double> expertWeight = ConvertUtil.convertExpertWeight(expertImportance);
		model.addObject("expertWeight", expertWeight);
		model.addObject("attributeWeight", attributeWeight);
		model.setViewName("show_weight");
		return model;
	}
	

}
